class Friends {
	friendsList = []
	notificationList = []
	userList = []
	groupList = []
	notificationUnreadCount = 0
	async getFriendsCmdMethods() {
		const {
			data
		} = await uni.$Im.executeCmdMethods(34, {});
		this.set(data);
		return data
	}
	/**
	 * 好友通知
	 * @param {Object} params
	 * 
	 */
	async getNotificationListCmdMethods(params) {
		const {
			data
		} = await uni.$Im.executeCmdMethods(28, params);
		this.updateNotificationList(data);
		this.updateNotificationUnreadCount();
		return data;
	}

	getNotificationList(params) {
		return this.notificationList;
	}

	updateNotificationUnreadCount() {
		this.notificationUnreadCount = this.notificationList.reduce((total, item) => {
			item.isRead == 0 && total++
			return total
		}, 0);
	}

	getNotificationUnreadCount() {
		return this.notificationUnreadCount;
	}

	updateNotificationList(data) {
		this.notificationList = data.map(item => {
			return {
				...item,
				isRead: 0,
				fromInfo: item.fromInfo && JSON.parse(item.fromInfo),
			}
		});
	}
	/**
	 * 添加好友
	 * @param {Object} params
	 * 
	 */
	async addFriendInvite(params) {
		const {
			data
		} = await uni.$Im.executeCmdMethods(29, params);
	}
	/**
	 * 同意好友
	 * @param {Object} params
	 * 
	 */
	async acceptFriendApplication(params, callback) {
		const {
			data
		} = await uni.$Im.executeCmdMethods(30, params);
		callback && callback(data);
		return data;
	}
	/**
	 *拒绝好友
	 * @param {Object} params
	 * 
	 */
	async refuseFriendApplication(params, callback) {
		const {
			data
		} = await uni.$Im.executeCmdMethods(31, params);
		callback && callback(data);
		return data;
	}
	/**
	 * 设置已读
	 * @param {Object} params
	 * 
	 */
	async readNotification(params) {
		const {
			data
		} = await uni.$Im.executeCmdMethods(27, params);
		return data;
	}

	/**
	 * 搜索用户列表的接口。
	 * @param data 输入自己需要的额外参数逻辑，可以不传
	 * @returns {*}
	 */
	async searchUserList(params, callback) {
		const {
			data
		} = await uni.$Im.executeCmdMethods(24, params);
		this.updateUserList(data)
		callback && callback(data);
		return data;
	}

	updateUserList(data) {
		this.userList = data;
	}

	/**
	 * 搜索群组列表的接口。
	 * @param data 输入自己需要的额外参数逻辑，可以不传
	 * @returns {*}
	 */
	async searchGroupList(params, callback) {
		const {
			data
		} = await uni.$Im.executeCmdMethods(25, params);
		callback && callback(data);
		this.updateGroupList(data)
		return data;
	}

	updateGroupList(data) {
		this.groupList = data;
	}
	//不区分大小写
	getUserList(words) {
		if (words) {
			return this.userList.filter(item => item.nick.toLowerCase().includes(words.toLowerCase()));
		} else {
			return this.userList;
		}
	}

	getGroupList(words) {
		if (words) {
			return this.groupList.filter(item => item.name.toLowerCase().includes(words.toLowerCase()));
		} else {
			return this.groupList;
		}
	}

	async addFriendInvite(params, callback) {
		const body = await uni.$Im.executeCmdMethods(29, params);
		const {
			data
		} = body;
		callback && callback(body);
		return data;
	}

	async addGroupInvite(params, callback) {
		const body = await uni.$Im.executeCmdMethods(36, params);
		const {
			data
		} = body;
		callback && callback(body);
		return data;
	}

	// 获取好友列表
	get(words) {
		if (words) {
			return this.friendList.filter(item => item.nick.toLowerCase().includes(words.toLowerCase()));
		} else {
			return this.friendList;
		}
	}

	set(data) {
		this.friendList = data;
	}
}
export default Friends